use "${clean_data}\gfs_cleaned_merged_database.dta", clear
rename * , lower

foreach x in  belonging	approve_govt	bodily_pain	capable	close_to	comfort_rel	connected_rel	content	control_worry	covid_death	critical	days_exercise	depressed	discriminated	donated	drinks	expect_good	expenses	feel_anxious	forgive	freedom	give_up	god_punish	grateful	group_not_rel	happy	health_prob	help_stranger	hope_future	interest	life_approach	life_balance	life_purpose	life_sat	lonely	loved_by_god	mental_health	peace	people_help	physical_hlth	promote_good	sat_live	say_in_govt	show_love	suffering	threat_life	trust_people	volunteered	wb_five	wb_today	worry_safety	worthwhile	income_feelings {
replace `x'=. if `x'==-98
replace `x'=. if `x'==98
replace `x'=. if `x'==99
}

foreach x in parents_12yrs svcs_12yrs svcs_father svcs_mother {
replace `x'=. if `x'==97
replace `x'=. if `x'==-98
replace `x'=. if `x'==98
replace `x'=. if `x'==99
}

ren annual_weight1  weight

foreach x in father_relatn mother_relatn father_loved mother_loved {
replace `x'=. if  `x'<1 |  `x'>=98
}

gen good_dad=4 if father_relatn==1
replace good_dad=3 if father_relatn==2
replace good_dad=2 if father_relatn==3
replace good_dad=1 if father_relatn==4
gen good_mom=4 if mother_relatn==1
replace good_mom=3 if mother_relatn==2
replace good_mom=2 if mother_relatn==3
replace good_mom=1 if mother_relatn==4

tab father_loved, gen(flove)
ren flove1 flove_yes
ren flove2 flove_no
ren flove3 flove_no_dad

tab mother_loved, gen(mlove)
ren mlove1 mlove_yes
ren mlove2 mlove_no
ren mlove3 mlove_no_mom

gen not_outsider_fam=1 if outsider==2
replace not_outsider_fam=0 if outsider==1
gen outsider_fam=1 if outsider==1
replace outsider_fam=0 if outsider==2

gen not_abused=1 if abused==2
replace not_abused=0 if abused==1

gen was_abused=1 if abused==1
replace was_abused=0 if abused==2

tab good_dad, gen(good_dad)
tab good_mom, gen(good_mom)

**Parent religiosity

*********
**PARENT-RELIGIOSITY
*******

foreach x in  svcs_father svcs_mother {
gen RE`x'=4 if `x'==1
replace RE`x'=3 if `x'==2
replace RE`x'=2 if `x'==3
replace RE`x'=1 if `x'==4
}

gen nev_attend_dad=svcs_father
replace nev_attend_dad=. if svcs_father<0
replace nev_attend_dad=. if svcs_father>4

gen nev_attend_mom=svcs_mother
replace nev_attend_mom=. if svcs_mother<0
replace nev_attend_mom=. if svcs_mother>4

gen parent_relig=1 if rel1>=1 & rel1<=96
replace parent_relig=0 if rel1==97

*parental relig index items 
sum parent_relig REsvcs_father REsvcs_mother

***
*Self-religion
***

gen self_not_relig=0 if rel2>=1 & rel2<=96
replace self_not_relig=1 if rel2==97

gen self_relig=1 if rel2>=1 & rel2<=96
replace self_relig=0 if rel2==97

gen relig_import=1 if rel_import==1
replace relig_import=0 if rel_import==2

gen attendance=1 if attend_svcs==5
replace attendance=2 if attend_svcs==4
replace attendance=3 if attend_svcs==3
replace attendance=4 if attend_svcs==2
replace attendance=5 if attend_svcs==1

*self-religiosity index items
sum relig_import attendance self_relig


**********
**other controls**
*********

foreach  i in income_feelings  {
gen RE`i'=4 if `i'==1
replace RE`i'=3 if `i'==2
replace RE`i'=2 if `i'==3
replace RE`i'=1 if `i'==4
}
tab REincome_feelings, gen(REincome_feelings)

desc education_3
label list EDUCATION_3
replace education_3=. if education_3<1 | education_3>3
tab education_3, gen(edu)
ren edu1 primary
ren edu2 secondary
ren edu3 tertiary

replace num_children=. if num_children<0 | num_children>97
egen children=cut(num_children),at(0,1,2,3,100)
tab children, gen(child)
ren child1  no_children
ren child2 one_child
ren child3 two_children
ren child4 three_plus_children

gen parents_married=1 if parents_12yrs==1
replace parents_married=0 if parents_12yrs>1 & parents_12yrs<=5

gen parent_died=0 if parents_12yrs>=1 & parents_12yrs<=5
replace parent_died=1 if parents_12yrs==4

gen child_poverty=income_12yrs
replace child_poverty=. if income_12yrs<1 | income_12yrs>4

gen female=1 if gender==2
replace female=0 if gender==1 | gender==3 | gender==4

gen male=1 if gender==1
replace male=0 if gender==2 | gender==3 | gender==4

gen other_gender=1 if gender==3 | gender==4
replace other_gender=0 if gender==1 | gender==2

gen employed=1 if employment==1  | employment==2
replace employed=0 if employment>2 & employment<=6

sum age
replace age=. if age>150
egen age_cat=cut(age), at(18,25,35,45,55,65,75,150)
tab age_cat, gen(age_group)
label var age_group1 "age 18-24"
label var age_group2 "age 25-34"
label var age_group3 "age 35-44"
label var age_group4 "age 45-54"
label var age_group5 "age 55-64"
label var age_group6 "age 65-74"
label var age_group7 "age 75 and older"

replace income=. if income>2413
replace income=. if income<0

bysort country: egen maxi=max(income)
bysort country: egen mini=min(income)
desc income
label list INCOME
sum income
gen hhinc=100*(income-mini)/(maxi-mini)
tab hhinc
gen lnhhinc=ln(hhinc)

gen live_alone=1 if num_household==1
replace live_alone=0 if num_household!=1
replace live_alone=. if num_household<0 | num_household>=97

gen foreign_born=1 if born_country==2
replace foreign_born=0 if born_country==1

label list MARITAL_STATUS
gen married=1 if marital_status==2
replace married=0 if marital_status!=2
replace married=. if marital_status>=98

gen never_married=1 if marital_status==1
replace never_married=0 if marital_status!=1
replace never_married=. if marital_status>=98

tab urban_rural
label list URBAN_RURAL
replace urban_rural=. if urban_rural<1 | urban_rural>4
tab urban_rural, gen(local)
ren local1 rural
ren local2 village
ren local3 city
ren local4 suburb

global demo "male other_gender secondary tertiary hhinc  age_group2 age_group3 age_group4 age_group5 age_group6 age_group7 foreign_born  employed never_married married live_alone no_children one_child two_children  rural village suburb"
global parent_demo "parents_married parent_died  flove_no_dad mlove_no_mom child_poverty was_abused"

label var male "Respondet is male"
label var female "Respondet is female"
label var other_gender "Did not select male or female"
label var primary "primary education"
label var secondary "secondary education"
label var	tertiary "tertiary education"
label var	hhinc	"household income-national rank"
label var	age_group1	"Ages 18-25"
label var	age_group2	"Ages 25-34"
label var	age_group3	"Ages 35-44"
label var	age_group4	"Ages 45-54"
label var	age_group5	"Ages 55-64"
label var	age_group6	"Ages 65-74"
label var	age_group7	"Ages 75 and older"
label var	foreign_born	"Born in another country"
label var	employed	"Currently working for pay"
label var	never_married	"Never married"
label var	married	"Married"
label var	live_alone	"Live alone"
label var	no_children	"Do not have children"
label var	one_child	"Has one child"
label var	two_children	"Has two children"
label var	three_plus_children	"Has three or more children"
label var	rural	"Live in rural area"
label var	village	"Live in village or small town"
label var	suburb	"Live in suburb of city"
label var parents_married "Parents were married at age 12" 
label var parent_died  "One or more parents died"
label var  flove_no_dad "Father was dead or unfamiliar"
label var  mlove_no_mom  "Mother was dead or unfamiliar"
label var  child_poverty "Level of finanical difficulty while growing up"
label var  was_abused "Respondent was abused sexually or physically"
label var REincome_feelings1 "Finding it very difficult on present income"
label var REincome_feelings2 "Finding it difficult on present income"
label var REincome_feelings3 "Getting by on present income" 
label var REincome_feelings4 "Living comfortably on present income"

tab child_poverty , gen(child_poverty)
tab depressed, gen(depressed)
gen happy7=1 if happy>=7 & happy<=10
replace happy7=0 if happy<7

gen thriving=1 if wb_fiveyrs>=8 & wb_today>=7
replace thriving=0 if wb_fiveyrs<8 | wb_today<7
replace thriving=. if wb_fiveyrs==. | wb_today==.

gen relation_structure="2 Lived with mom but not dad" if father_loved==97 & (mother_loved==2 | mother_loved==1)
replace relation_structure="4 Did not live with mom or dad" if father_loved==97 & mother_loved==97
replace relation_structure="3 Lived with dad but not mom" if mother_loved==97 & (father_loved==2 | father_loved==1)
replace relation_structure="1 Lived with mom and dad" if (mother_loved==2 | mother_loved==1) & (father_loved==2 | father_loved==1)
replace relation_structure="1 Lived with mom and dad" if mother_loved<97 & father_loved<97

tab relation_structure, gen(living_structure)
encode relation_structure, gen(living_arrangement)
label var living_structure1 "Knew mom and dad"
label var living_structure2 "Knew mom but not dad"
label var living_structure3 "Knew dad but not mom"
label var living_structure4 "Did not know mom or dad"

**SUPPLEMENTAL TABLE 9
tabstat thriving happy7 depressed1 depressed2 depressed3 depressed4 ///
living_structure1 living_structure2 living_structure3 living_structure4 ///
hope_future	content	grateful	promote_good	worthwhile	sat_relatnshp	expect_good	wb_fiveyrs	show_love	life_purpose	happy	freedom	give_up	life_sat	people_help	wb_today	mental_health	physical_hlth	belonging ///
feel_anxious	control_worry	depressed	suffering	interest	bodily_pain	threat_life ///
outsider_fam flove_yes flove_no  mlove_yes mlove_no   good_dad1 good_dad2 good_dad3 good_dad4 good_mom1 good_mom2 good_mom3 good_mom4  ///
parent_relig REsvcs_father REsvcs_mother relig_import attendance self_relig ///
female male other_gender primary secondary tertiary age_group1 age_group2 age_group3 age_group4 age_group5 age_group6 age_group7 foreign_born  employed never_married married live_alone no_children one_child two_children three_plus_children rural village suburb city ///
REincome_feelings1 REincome_feelings2 REincome_feelings3 REincome_feelings4 parents_married was_abused child_poverty1 child_poverty2 child_poverty3 child_poverty4  [aw=weight], stat(n mean sd  ) col(stat)

**Parent death vs exit
*Evidence that it is better to have no relation with a parent ("does not apply") than a bad one
sum good_mom good_dad flove_yes  mlove_ye flove_no mlove_no flove_no_dad mlove_no_mom not_outsider was_abused [aw=weight]
bysort father_relatn: sum good_mom good_dad flove_yes flove_no mlove_no flove_no_dad mlove_no_mom mlove_ye not_outsider was_abused [aw=weight]
bysort mother_relatn: sum good_mom good_dad flove_yes flove_no mlove_no flove_no_dad mlove_no_mom mlove_ye not_outsider was_abused [aw=weight]
bysort parent_died: sum good_mom good_dad flove_yes flove_no mlove_no flove_no_dad mlove_no_mom mlove_ye not_outsider was_abused [aw=weight]

**Country-level summary status, Supplemental T10

use "${output}\gfs_cleaned_merged_database.dta", clear

tab feel_anxious [aw=weight]

tab father_loved, gen(flove)
ren flove1 flove_yes_raw
ren flove2 flove_no_raw
ren flove3 flove_no_dad_raw

tab mother_loved, gen(mlove)
ren mlove1 mlove_yes_raw
ren mlove2 mlove_no_raw
ren mlove3 mlove_no_mom_raw

gen bothered_by_anxiety=1 if feel_anxious<-1
replace bothered_by_anxiety=0 if feel_anxious>-1 & feel_anxious<1

drop relig_import
gen relig_import=1 if rel_import==1
replace relig_import=0 if rel_import==2

tab rel2
gen sect=rel2
replace sect=0 if self_not_relig==1
replace sect=. if rel2>96
replace sect=. if rel2<0
tab sect, gen(sect)

collapse   (count)  Nflourish=HOPE Nfl=father_loved Nabuse=was_abused ///
(mean)  male age_group1 age_group2 age_group3 age_group4 age_group5 age_group6 age_group7 ///
mlove_yes_raw mlove_no_raw mlove_no_mom_raw flove_yes_raw flove_no_raw flove_no_dad_raw was_abused thriving bothered_by_anxiety relig_import  ///
sect1 sect2 sect3 sect4 sect5 sect6 sect7 sect8 sect9 sect10 sect11 sect12 sect13 sect14 sect15 sect16  ///
(first) gdp_pc_max v2x_polyarchy zwvs_traditional zwps_index lngdp [aw=weight], by(country_name)

cor v2x_polyarchy zwvs_traditional zwps_index lngdp
factor v2x_polyarchy zwvs_traditional zwps_index lngdp

**Calculate 
forval i=1/16 {
	gen sect`i'_sq=(sect`i')^2
}
egen SUM=rowtotal(sect1_sq sect2_sq sect3_sq sect4_sq sect5_sq sect6_sq sect7_sq sect8_sq sect9_sq sect10_sq sect11_sq sect12_sq sect13_sq sect14_sq sect15_sq sect16_sq)

drop sect1 sect2 sect3 sect4 sect5 sect6 sect7 sect8 sect9 sect10 sect11 sect12 sect13 sect14 sect15 sect16 sect1_sq sect2_sq sect3_sq sect4_sq sect5_sq sect6_sq sect7_sq sect8_sq sect9_sq sect10_sq sect11_sq sect12_sq sect13_sq sect14_sq sect15_sq sect16_sq

*Generate diversity index
gen DIV=1-SUM
sum DIV
format DIV %15.5f

gsort - mlove_yes_raw
export delimited "${output}\Country_Level_Summary.csv", replace

keep country DIV
label var DIV "Religious diveristy index"
gsort -DIV
edit
save "${output}\religious_diversity_by_country.dta", replace

